// pages/IOT/guard.js
import mqtt from '../../utils/mqtt.js';

//连接的服务器域名，注意格式！！！
const host = 'wxs://www.guanzhuoran.cn/mqtt';


Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    name: "",
    stime: "",
    etime: "",
    meetroom: "",
    sendtopic: "",
    showmark: 0,


    client: null,
    //记录重连的次数
    reconnectCounts: 0,
    //MQTT连接的配置
    sendtopic: "",
    options: {
      protocolVersion: 4, //MQTT连接协议版本
      clientId: 'miniTest',
      clean: false,
      password: '116648728gG',
      username: 'admin',
      reconnectPeriod: 1000, //1000毫秒，两次重新连接之间的间隔
      connectTimeout: 30 * 1000, //1000毫秒，两次重新连接之间的间隔
      resubscribe: true, //如果连接断开并重新连接，则会再次自动订阅已订阅的主题（默认true）
      port: 8084,
    },

  },

  bindMeetingMarkChange: function () {
    var that = this
    var url = wx.getStorageSync("config")

    wx.showModal({
      title: '删除确认',
      content: '请核对删除内容是否正确',
      success: function (res) {
        if (res.confirm) {

          wx.request({
            url: url.service.homeUrl + '/meeting/changemeetingmark',
            data: {
              id: that.data.id,
              mark: 0
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {

              wx.showToast({
                title: res.data.msg,
              })
              wx.navigateTo({
                url: '../meeting/meetingorder',
              })

            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
          })



        }
      }
    })
  },


  onClick_PubMsg: function () {
    if (this.data.client && this.data.client.connected) {
      this.data.client.publish(this.data.sendtopic, '1');
      wx.showToast({
        title: '会议室门禁已开启（5️秒后自动关闭，请尽快进入会议室）'
      })
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var url = wx.getStorageSync("config")
    this.setData({
      id: options.id
    })
    wx.request({
      url: url.service.homeUrl + '/meeting/getmeetingorderdetail',
      data: {
        id: options.id
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        that.setData({
          name: res.data.name,
          stime: res.data.stime,
          etime: res.data.etime,
          meetingroom: res.data.meetingroom,
          sendtopic: res.data.topic,
          showmark: res.data.showmark
        })

        // 开始连接
        this.data.client = mqtt.connect(host, this.data.options);
        this.data.client.on('connect', function (connack) {
          let showm = that.data.showmark + 1
          console.log(showm)
          that.setData({
            showmark: showm
          })
        })



        //服务器连接异常的回调
        that.data.client.on("error", function (error) {
          console.log(" 服务器 error 的回调" + error)

        })

        //服务器重连连接异常的回调
        that.data.client.on("reconnect", function () {
          console.log(" 服务器 reconnect的回调")

        })


        //服务器连接异常的回调
        that.data.client.on("offline", function (errr) {
          console.log(" 服务器offline的回调")

        })

      },
      fail: function (res) {},
      complete: function (res) {},
    })


  },

  changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})